import Vue from 'vue'
import Router from 'vue-router'
import HomePage from './views/HomePage.vue'
// import TypePage from './views/TypePage.vue'
// import ProductsPage from './views/ProductsPage.vue'
// import AboutPage from './views/AboutPage.vue'
// import LoginPage from './views/LoginPage.vue'
// import DetailPage from './views/DetailPage.vue'
// import CartPage from './views/CartPage.vue'

Vue.use(Router)

const privateFun = (to, from, next)=>{
    next();
}
const router = new Router({
    linkActiveClass: "active",
    linkExactActiveClass: "exactActive",
    routes: [
        // component 方法定义方式
        //    component: () => import(/* webpackChunkName: "group-foo" */ './Foo.vue')
        { path: "/", redirect: "/home" },
        { path: "/home", name: "home", component: HomePage },
        { path: "/type", name: "type", component: () => import(/* webpackChunkName: "type" */ './views/TypePage.vue') },
        { path: "/list/:fcid?/:title?", name: "list",component: () => import(/* webpackChunkName: "list" */ './views/ProductsPage.vue'), meta: { hideBottom: true, showMenu: true } },
        { path: "/about", name: "about", component: () => import(/* webpackChunkName: "about" */ './views/AboutPage.vue'), 
          meta: { hideTop: true,private:true },
        //   beforeEnter:privateFun
        },
        { path: "/login", name: "login", component: () => import(/* webpackChunkName: "login" */ './views/LoginPage.vue'), meta: { hideTop: true, hideBottom: true, showMenu: false } },
        { path: "/detail/:iid", name: "detail", component: () => import(/* webpackChunkName: "detail" */ './views/DetailPage.vue'), meta: { hideTop: true, hideBottom: true } },
        { path: "/cart", name: "cart", component: () => import(/* webpackChunkName: "cart" */ './views/CartPage.vue'), meta: { hideTop: true, hideBottom: true,private:true  },
        //   beforeEnter:privateFun 
        },

    ]
})
/*
    在 sessionStorage 中提供一个 用于表示用户登录的关键字 token

*/
router.beforeEach((to,from,next)=>{
    if(!to.meta.private){
        next();
        return;
    }
    let token = sessionStorage.getItem("token");
    if(token===null||token===undefined||token===""){
       next("/login");
    }else{
        next();
    }
    // console.log("beforeEach");
})

// router.afterEach((to,from)=>{
//     console.log("afterEach");
// })

export default router;
